/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
more details.
**********/


#include "bitstream.h"
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>         /* For _MAX_PATH definition */
#include <malloc.h>

/////////////////////////////////////
uint32_t *bitstream;
///////////////////////
/* reads n bits from bitstream without changing the stream pos */

BitStream::BitStream()
{

}

BitStream::~BitStream()
{

}

uint32_t BitStream::BitstreamShowBits(Bitstream * bs,uint32_t bits)
{
	int nbit = (bits + bs->pos) - 32;
	uint32_t ret_value;
	
	if (nbit > 0) 
	{
		ret_value =  ((bs->bufa & (0xffffffff >> bs->pos)) << nbit) |
				(bs->bufb >> (32 - nbit));
	}
	else 
	{
		ret_value = (bs->bufa & (0xffffffff >> bs->pos)) >> (32 - bs->pos - bits);
	}
	//	fprintf(stderr,"show 2 bufa :%x; bufb :%x ;pos :%u;\n",bs->bufa,bs->bufb,bs->pos);
	//	fprintf(stderr,"showbits %d %x\n", bits, ret_value);
	
	return ret_value;
}

/* skip n bits forward in bitstream */
void BitStream::BitstreamSkip(Bitstream * bs,uint32_t bits)
{
	uint32_t tmp;
	bs->pos += bits;
	if (bs->pos >= 32) 
	{
		bs->bufa = bs->bufb;
		tmp = *((uint32_t *)bs->tail + 2);
		bs->bufb = tmp;
		bs->tail++;
		bs->pos -= 32;
	}
	//fprintf(stderr,"skip 2 bufa :%x; bufb :%x ;pos :%u;\n",bs->bufa,bs->bufb,bs->pos);
	//fprintf(stderr,"used %d\n", bits);
}

/* bitstream length (unit bits) */
uint32_t BitStream::BitstreamPos(Bitstream * bs)
{
    return 8 * ((uint32_t)bs->tail - (uint32_t)bs->start) + bs->pos;
}

/*	flush the bitstream & return length (unit bytes)
	NOTE: assumes no futher bitstream functions will be called.*/
uint32_t BitStream::BitstreamLength(Bitstream * bs)
{
	uint32_t len = (uint32_t) bs->tail - (uint32_t) bs->start;
	uint32_t b;
    if (bs->pos)
    {
		b = bs->buf;
		*bs->tail = b;

		len += (bs->pos + 7) / 8;
    }

	return len;
}

/* read n bits from bitstream */
uint32_t BitStream::BitstreamGetBits(Bitstream * bs,uint32_t n)
{
	uint32_t ret = BitstreamShowBits(bs, n);
	BitstreamSkip(bs, n);
	return ret;
}

/* read single bit from bitstream */
uint32_t BitStream::BitstreamGetBit(Bitstream * bs)//(Bitstream * const bs)  by wangyu
{
	return BitstreamGetBits(bs, 1);
}

int BitStream::log2bin(int value)
{
	int n = 0;
	while (value)
	{
		value >>= 1;
		n++;
	}
	return n;
}

static const uint32_t intra_dc_threshold_table[] =
{
	32,	/* never use */
	13,
	15,
	17,
	19,
	21,
	23,
	1,
};

void BitStream::BitstreamByteAlign(Bitstream *bs)
{
	uint32_t remainder = bs->pos % 8;
	if (remainder)
	{
		BitstreamSkip(bs, 8 - remainder);
	}
}

/*decode headers
returns coding_type, or -1 if error*/
int BitStream::BitStreamReadHeader(Bitstream * bs, XVID_DEC_PARAM * param,int findvol)
{
	uint32_t rounding; 
	uint32_t quant;
	uint32_t fcode;
	uint32_t intra_dc_threshold;
	static uint32_t vol_ver_id = 0;
	static uint32_t time_inc_resolution = 0;
	static uint32_t time_inc_bits = 0;
	static uint32_t coding_type = 0;
	static uint32_t fixed_vop_time_increment = 0;
	uint32_t start_code;
	static uint32_t width = 0, height = 0;
	static uint32_t shape = 0;
	static uint32_t interlacing = 0;
	static uint32_t top_field_first = 0;
	static uint32_t alternate_vertical_scan = 0;
	static uint32_t quant_bits = 0;
	static uint32_t quant_type = 0;
	static uint32_t quarterpel = 0;
	static uint32_t have_short_header = 0;
	int hours, minutes, seconds;
	uint32_t temp_width=0, temp_height=0;
	uint32_t horiz_mc_ref, vert_mc_ref;
	uint32_t source_format;
	
	do
	{
		BitstreamByteAlign(bs);
		start_code = BitstreamShowBits(bs, 32);
//		fprintf(stderr,"1 start_code is %x\n",start_code);
		//BitstreamSkip(bs,32);
		
		if (start_code == VISOBJSEQ_START_CODE)
		{
			BitstreamSkip(bs, 32);				// visual_object_sequence_start_code
			BitstreamSkip(bs, 8);					// profile_and_level_indication
		}
		else if (start_code == VISOBJSEQ_STOP_CODE)
		{
			BitstreamSkip(bs, 32);				// visual_object_sequence_stop_code
		}
		else if (start_code == VISOBJ_START_CODE)
		{
			BitstreamSkip(bs,32);					// visual_object_start_code
			if (BitstreamGetBit(bs))				// is_visual_object_identified
			{
				vol_ver_id = BitstreamGetBits(bs,4);	// visual_object_ver_id
				BitstreamSkip(bs, 3);				// visual_object_priority
			}
			else
			{
				vol_ver_id = 1;
			}

			if (BitstreamShowBits(bs, 4) != VISOBJ_TYPE_VIDEO)	// visual_object_type
			{
//				DEBUG("visual_object_type != video");
				return -1;
			}
			BitstreamSkip(bs, 4);

			// video_signal_type

			if (BitstreamGetBit(bs))				// video_signal_type
			{
	//			DEBUG("+ video_signal_type");
				BitstreamSkip(bs, 3);				// video_format
				BitstreamSkip(bs, 1);				// video_range
				if (BitstreamGetBit(bs))			// color_description
				{
		//			DEBUG("+ color_description");
					BitstreamSkip(bs, 8);			// color_primaries
					BitstreamSkip(bs, 8);			// transfer_characteristics
					BitstreamSkip(bs, 8);			// matrix_coefficients
				}
			}
		}
		else if ((start_code & ~0x1f) == VIDOBJ_START_CODE)
		{
			fprintf(stderr,"video_object_start_code \n");
			BitstreamSkip(bs, 32);		// video_object_start_code
			
		} 
		else if ((start_code & ~0xf) == VIDOBJLAY_START_CODE)
		{
			// DEBUG("video_object_layer");
			BitstreamSkip(bs, 32);					// video_object_layer_start_code

			BitstreamSkip(bs, 1);									// random_accessible_vol

			// video_object_type_indication
			if (BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_SIMPLE &&
				BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_CORE &&
				BitstreamShowBits(bs, 8) != VIDOBJLAY_TYPE_MAIN &&
				BitstreamShowBits(bs, 8) != 0)		// BUGGY DIVX
			{
			//	DEBUG1("video_object_type_indication not supported", BitstreamShowBits(bs, 8));
				fprintf(stderr,"video_object_type_indication not supported %d\n", BitstreamShowBits(bs, 8));
				return -1;
			}
			BitstreamSkip(bs, 8);


			if (BitstreamGetBit(bs))					// is_object_layer_identifier
			{
				//DEBUG("+ is_object_layer_identifier");
				vol_ver_id = BitstreamGetBits(bs,4);		// video_object_layer_verid
				BitstreamSkip(bs, 3);					// video_object_layer_priority
			}
			else
			{
				vol_ver_id = 1;
			}
			//DEBUGI("vol_ver_id", vol_ver_id);

			if (BitstreamGetBits(bs, 4) == VIDOBJLAY_AR_EXTPAR)	// aspect_ratio_info
			{
				//DEBUG("+ aspect_ratio_info");
				BitstreamSkip(bs, 8);						// par_width
				BitstreamSkip(bs, 8);						// par_height
			}

			if (BitstreamGetBit(bs))		// vol_control_parameters
			{
				//DEBUG("+ vol_control_parameters");
				BitstreamSkip(bs, 2);						// chroma_format
				BitstreamSkip(bs, 1);						// low_delay
				if (BitstreamGetBit(bs))					// vbv_parameters
				{
					//DEBUG("+ vbv_parameters");
					BitstreamSkip(bs, 15);				// first_half_bitrate
					READ_MARKER();
					BitstreamSkip(bs, 15);				// latter_half_bitrate
					READ_MARKER();
					BitstreamSkip(bs, 15);				// first_half_vbv_buffer_size
					READ_MARKER();
					BitstreamSkip(bs, 3);					// latter_half_vbv_buffer_size
					BitstreamSkip(bs, 11);				// first_half_vbv_occupancy
					READ_MARKER();
					BitstreamSkip(bs, 15);				// latter_half_vbv_occupancy
					READ_MARKER();
				}
			}

			shape = BitstreamGetBits(bs, 2);	// video_object_layer_shape
			// DEBUG1("shape", dec->shape);
			
			if (shape == VIDOBJLAY_SHAPE_GRAYSCALE && vol_ver_id != 1)
			{
				BitstreamSkip(bs, 4);		// video_object_layer_shape_extension
			}

			READ_MARKER();

			time_inc_resolution = BitstreamGetBits(bs, 16);	// vop_time_increment_resolution
			//time_inc_resolution--;
			fprintf(stderr,"time_inc_resolution is %u\n",time_inc_resolution);
			if (time_inc_resolution > 0)
			{
				time_inc_bits = log2bin(time_inc_resolution);
			}
			else
			{
				// dec->time_inc_bits = 0;

				// for "old" xvid compatibility, set time_inc_bits = 1
				time_inc_bits = 1;
			}
			READ_MARKER();

			if (BitstreamGetBit(bs))						// fixed_vop_rate
			{
				//BitstreamSkip(bs, time_inc_bits);	// fixed_vop_time_increment
				fixed_vop_time_increment = BitstreamGetBits(bs, time_inc_bits);	// fixed_vop_time_increment
				param->framerate = time_inc_resolution*1000/fixed_vop_time_increment;
				fixed_vop_rate = 1;
			}
			else
			{
				param->framerate = time_inc_resolution*1000/1001;
			}

			if (shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
			{

				if (shape == VIDOBJLAY_SHAPE_RECTANGULAR)
				{
					READ_MARKER();
					temp_width = BitstreamGetBits(bs, 13);			// video_object_layer_width
					//DEBUGI("width", width);
					//fprintf(stderr,"width is %u\n",width);
					READ_MARKER();
					temp_height = BitstreamGetBits(bs, 13);		// video_object_layer_height
					//DEBUGI("height", height);	
					//fprintf(stderr,"height is %u\n",height);
					READ_MARKER();

					if (findvol == 0) 
					{
					  if (temp_width != width || temp_height != height)
					    {
					      fprintf(stderr,"FATAL: video dimension discrepancy ***");
					      fprintf(stderr,"bitstream width %u /height %u\n", temp_width, temp_height);
					      fprintf(stderr,"param width %u /height %u\n", width, height);
					      return -1;
					    }
					} 
					else 
					{
					  width = temp_width;
					  height = temp_height;
					}
				}

				if ((interlacing = BitstreamGetBit(bs)))
				{
					fprintf(stderr,"vol: interlacing\n");
				}

				if (!BitstreamGetBit(bs))				// obmc_disable
				{
					fprintf(stderr,"IGNORED/TODO: !obmc_disable\n");
					// TODO
					// fucking divx4.02 has this enabled
				}

				if (BitstreamGetBits(bs, (vol_ver_id == 1 ? 1 : 2)))  // sprite_enable
				{
					fprintf(stderr,"sprite_enable; not supported\n");
					return -1;
				}
			
				if (vol_ver_id != 1 && shape != VIDOBJLAY_SHAPE_RECTANGULAR)
				{
					BitstreamSkip(bs, 1);					// sadct_disable
				}

				if (BitstreamGetBit(bs))						// not_8_bit
				{
					//DEBUG("+ not_8_bit [IGNORED/TODO]");
					quant_bits = BitstreamGetBits(bs, 4);	// quant_precision
					BitstreamSkip(bs, 4);						// bits_per_pixel
				}
				else
				{
					quant_bits = 5;
				}

				if (shape == VIDOBJLAY_SHAPE_GRAYSCALE)
				{
					BitstreamSkip(bs, 1);			// no_gray_quant_update
					BitstreamSkip(bs, 1);			// composition_method
					BitstreamSkip(bs, 1);			// linear_composition
				}

				quant_type = BitstreamGetBit(bs);		// quant_type
				// DEBUG1("**** quant_type", dec->quant_type);

				if (quant_type)
				{
					if (BitstreamGetBit(bs))		// load_intra_quant_mat
					{
					}
					else
						//set_intra_matrix(get_default_intra_matrix());

					if (BitstreamGetBit(bs))		// load_inter_quant_mat
					{
					}
					else
						//set_inter_matrix(get_default_inter_matrix());

					if (shape == VIDOBJLAY_SHAPE_GRAYSCALE)
					{
						// TODO
						fprintf(stderr,"TODO: grayscale matrix stuff\n");
						return -1;
					}
				}

			
				if (vol_ver_id != 1)
				{
					quarterpel = BitstreamGetBit(bs);	// quarter_sampe
					if (quarterpel)
					{
						fprintf(stderr,"IGNORED/TODO: quarter_sample\n");
					}
				}
				else
				{
					quarterpel = 0;
				}

				if (!BitstreamGetBit(bs))			// complexity_estimation_disable
				{
					fprintf(stderr,"TODO: complexity_estimation header\n");
					// TODO
					return -1;
				}

				if (!BitstreamGetBit(bs))			// resync_marker_disable
				{
					fprintf(stderr,"IGNORED/TODO: !resync_marker_disable\n");
					// TODO
				}

				if (BitstreamGetBit(bs))		// data_partitioned
				{
					fprintf(stderr,"+ data_partitioned\n");
					BitstreamSkip(bs, 1);		// reversible_vlc
				}

				if (vol_ver_id != 1)
				{
					if (BitstreamGetBit(bs))			// newpred_enable
					{
						fprintf(stderr,"+ newpred_enable\n");
						BitstreamSkip(bs, 2);			// requested_upstream_message_type
						BitstreamSkip(bs, 1);			// newpred_segment_type
					}
					if (BitstreamGetBit(bs))			// reduced_resolution_vop_enable
					{
						fprintf(stderr,"TODO: reduced_resolution_vop_enable\n");
						return -1;
					}
				}
				
				if (BitstreamGetBit(bs))	// scalability
				{
					fprintf(stderr,"TODO: scalability\n");
					return -1;
				}
			}
			else	// dec->shape == BINARY_ONLY
			{
				if (vol_ver_id != 1)
				{
					if (BitstreamGetBit(bs))	// scalability
					{
						fprintf(stderr,"TODO: scalability\n");
						return -1;
					}
				}
				BitstreamSkip(bs, 1);			// resync_marker_disable
			}
			param->width = width;
			param->height = height;
			param->time_inc_bits  = time_inc_bits;
			param->framerate = (time_inc_resolution+1)*1000/1001;
			return 1;
		}
		else if (start_code == GRPOFVOP_START_CODE)
		{
			// DEBUG("group_of_vop");
			BitstreamSkip(bs, 32);
			{
				
				hours = BitstreamGetBits(bs, 5);
				minutes = BitstreamGetBits(bs, 6);
				READ_MARKER();
				seconds = BitstreamGetBits(bs, 6);
				// DEBUG3("hms", hours, minutes, seconds);
			}
			BitstreamSkip(bs, 1);			// closed_gov
			BitstreamSkip(bs, 1);			// broken_link
		}
		else if (start_code == VOP_START_CODE)
		{
		  if (findvol != 0) return -1;
			// DEBUG("vop_start_code");
			BitstreamSkip(bs, 32);						// vop_start_code

			coding_type = BitstreamGetBits(bs, 2);		// vop_coding_type
			//DEBUG1("coding_type", coding_type);

			while (BitstreamGetBit(bs) != 0) ;			// time_base
	
			READ_MARKER();
	 
			//DEBUG1("time_inc_bits", dec->time_inc_bits);
			//DEBUG1("vop_time_incr", BitstreamShowBits(bs, dec->time_inc_bits));
			if (time_inc_bits)
			{
				BitstreamSkip(bs, time_inc_bits);	// vop_time_increment
			}

			READ_MARKER();

			if (!BitstreamGetBit(bs))					// vop_coded
			{
				return N_VOP;
			}

		
			if (coding_type != I_VOP)
			{
				rounding = BitstreamGetBit(bs);	// rounding_type
				//DEBUG1("rounding", *rounding);
			}

			if (shape != VIDOBJLAY_SHAPE_RECTANGULAR)
			{
							
				temp_width = BitstreamGetBits(bs, 13);
				READ_MARKER();
				temp_height = BitstreamGetBits(bs, 13);
				READ_MARKER();
				horiz_mc_ref = BitstreamGetBits(bs, 13);
				READ_MARKER();
				vert_mc_ref = BitstreamGetBits(bs, 13);
				READ_MARKER();

				// DEBUG2("vop_width/height", width, height);
				// DEBUG2("ref             ", horiz_mc_ref, vert_mc_ref);

				BitstreamSkip(bs, 1);				// change_conv_ratio_disable
				if (BitstreamGetBit(bs))			// vop_constant_alpha
				{
					BitstreamSkip(bs, 8);			// vop_constant_alpha_value
				}
			}
				

			if (shape != VIDOBJLAY_SHAPE_BINARY_ONLY)
			{
				// intra_dc_vlc_threshold
				intra_dc_threshold = intra_dc_threshold_table[ BitstreamGetBits(bs,3) ];

				if (interlacing)
				{
					if ((top_field_first = BitstreamGetBit(bs)))
					{
						fprintf(stderr,"vop: top_field_first\n");
					}
					if ((alternate_vertical_scan = BitstreamGetBit(bs)))
					{
						fprintf(stderr,"vop: alternate_vertical_scan");
					}
				}
			}
						
			quant = BitstreamGetBits(bs, quant_bits);		// vop_quant
			//DEBUG1("quant", *quant);
						
			if (coding_type != I_VOP)
			{
				fcode = BitstreamGetBits(bs, 3);			// fcode_forward
			}
				
			if (coding_type == B_VOP)
			{
				// *fcode_backward = BitstreamGetBits(bs, 3);		// fcode_backward
			}
			return coding_type;
		}
		else if (start_code == USERDATA_START_CODE)
		{
			// DEBUG("user_data");
			BitstreamSkip(bs, 32);		// user_data_start_code
		}
		else if ((start_code & 0xfffffc03) == 0x00008002) 
		{
		  // Short video header.  Skip short_video_start_marker,
		  // temporal reference, marker and zero bit
		  have_short_header = 1;
		  shape = VIDOBJLAY_SHAPE_RECTANGULAR;
		  interlacing = 0;
		  quant_bits = 5;
		  quant_type = 0;
		  quarterpel = 0;
		  intra_dc_threshold = intra_dc_threshold_table[0];
		  rounding = 0;
		  fcode = 1;
		  BitstreamSkip(bs, 22);
		  BitstreamSkip(bs, 8 + 5);
		  source_format = BitstreamGetBits(bs, 3);
		  switch (source_format) {
		  case 1: // sub-QCIF
		    width = 128;
		    height = 96;
		    break;
		  case 2: // QCIF
		    width = 176;
		    height = 144;
		    break;
		  case 3: // CIF
		    width = 352;
		    height = 288;
		    break;
		  case 4: // 4CIF
		    width = 704;
		    height = 576;
		    break;
		  case 5:
		    width = 1408;
		    height = 1152;
		    break;
		  default:
		    fprintf(stderr,"FATAL: illegal code in short video header %u\n", source_format);
		    return -1;
		  }
		  if (findvol == 0) {
		    if (temp_width != width || temp_height != height)
		      {
						fprintf(stderr,"FATAL: video dimension discrepancy ***");
						fprintf(stderr,"bitstream width %u /height %u \n", width, height);
						fprintf(stderr,"param width %u /height %u \n", width, height);
						return -1;
		      }
		  } 
			else 
			{
		    width = temp_width;
		    height = temp_height;
				param->width = width;
				param->height = height;
		    return 1;
		  }
		  if (BitstreamGetBit(bs)) 
			{
		    // P frame
		    coding_type = P_VOP;
		  }
			else 
			{
		    coding_type = I_VOP;
		  }
		  BitstreamSkip(bs, 4); // skip 4 reserved 0 bits
		  quant = BitstreamGetBits(bs, 5);
		  BitstreamSkip(bs, 1);
		  while (BitstreamGetBit(bs) == 1) BitstreamSkip(bs, 8); // pei and psupp
		  return coding_type;
		}
		else  // start_code == ?
		{
			if (BitstreamShowBits(bs, 24) == 0x000001)
			{
				fprintf(stderr,"*** WARNING: unknown start_code%u\n", BitstreamShowBits(bs, 32));
			}
			BitstreamSkip(bs, 8);
		}
	}
	while ((BitstreamPos(bs) >> 3) < bs->length);

	fprintf(stderr,"*** WARNING: no vop_start_code found");
	if (findvol != 0) return 0;
	return -1; /* ignore it */
}

/* initialise bitstream structure */
void BitStream::BitstreamInit(Bitstream * bs,/*const uint32_t * bitstream,*/uint32_t length)
{
	uint32_t tmp;
	bs->start = bs->tail = (uint32_t*)bitstream;
	
	tmp = *(uint32_t *)bitstream;
	bs->bufa = tmp;
	tmp = *((uint32_t *)bitstream+1);

	bs->bufb = tmp;

	bs->buf = 0;
	bs->pos = 0;
	bs->length = length;
}

// entire function added for mpeg4ip
int BitStream::decoder_find_vol( uint32_t length,XVID_DEC_PARAM * param)
{
	Bitstream bs;
	int ret;
	BitstreamInit(&bs,length);

	ret = BitStreamReadHeader(&bs,param,1);
	return ret;
}

int BitStream::parse_vovod(unsigned char *ConfigHex,unsigned int HexLen,unsigned *FrameRate,unsigned *TimeIncBits,unsigned *Width,unsigned *Height)
{
	int ret = -1;
	int size_int = 0;
	int i = 0,j = 0;
	XVID_DEC_PARAM param;

	// Get the VO/VOL header.  If we fail, set the bytestream back
	size_int = HexLen/sizeof(uint32_t)+1;
	bitstream = (uint32_t *)malloc(size_int*sizeof(uint32_t));
	if(bitstream == NULL)
	{
		fprintf(stderr,"bitstream alloc failed \n");
		return ret;	
	}
	for(i=0;i<size_int;i++)
	{
		bitstream[i] = ((ConfigHex[j]&0xff)<<24)|((ConfigHex[j+1]&0xff)<<16)|((ConfigHex[j+2]&0xff)<<8)|(ConfigHex[j+3]&0xff);
		j = j+sizeof(uint32_t);
		fprintf(stderr,"bitstream i %d,%x\n",i,*(bitstream+i));
	}

	ret = 0;
	ret = decoder_find_vol(HexLen, &param);
	*Width = param.width;
	*Height = param.height;
	*TimeIncBits = param.time_inc_bits;
	*FrameRate = param.framerate;
	free(bitstream);
	bitstream = NULL;

	return ret;
}
